package com.ermax.aircond.maintain.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ermax.aircond.maintain.dao.RoomDao;
import com.ermax.aircond.maintain.domain.Building;
import com.ermax.aircond.maintain.domain.Floor;
import com.ermax.aircond.maintain.domain.Room;
import com.ermax.aircond.util.dao.AbstractHibernateDao;

@Repository("roomDao")
public class RoomDao extends AbstractHibernateDao<Room> implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	public RoomDao() {
		setClazz(Room.class);
	}
	
	public List<Room> findAll(String status){
		Criteria criteria = this.getCurrentSession().createCriteria(Room.class);
		
		if(isNotNull(status)){
			criteria.add(Restrictions.eq("status", status));
		}

		criteria.addOrder(Order.asc("roomId"));
		criteria.addOrder(Order.asc("roomNo"));		
		criteria.addOrder(Order.asc("roomName"));
		
		@SuppressWarnings("unchecked")
		List<Room> results = criteria.list();
		return results;
	}

	public List<Room> findRooms(Building building, Floor floor, String keyword,String status) {
		Criteria criteria = this.getCurrentSession().createCriteria(Room.class);

		if (isNotNull(building)) {
			criteria.createCriteria("floor").add(Restrictions.eq("building", building));
		}

		if (isNotNull(floor)) {
			criteria.add(Restrictions.eq("floor", floor));
		}

		if (isNotNull(keyword)) {
			StringBuilder sbKey = new StringBuilder();
			sbKey.append("%");
			sbKey.append(keyword);
			sbKey.append("%");
			String key = sbKey.toString();

			criteria.add(Restrictions.like("roomName", key).ignoreCase());
			criteria.add(Restrictions.like("description", key).ignoreCase());
		}
		
		if(isNotNull(status)){
			criteria.add(Restrictions.eq("status", status));
		}
		
		criteria.addOrder(Order.asc("roomId"));
		criteria.addOrder(Order.asc("roomNo"));		
		criteria.addOrder(Order.asc("roomName"));

		@SuppressWarnings("unchecked")
		List<Room> results = criteria.list();
		return results;
	}

}
